package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/4/12.
 */

public class SizeViewGroup extends ViewGroup {
    public SizeViewGroup(Context context) {
        this(context, null);
    }

    public SizeViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SizeViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //创建一个按钮
        TextView textView = new TextView(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(200, 200);
        textView.setText("Android");
        textView.setBackgroundColor(Color.YELLOW);
        //在当前容器中添加子组件
        addView(textView);
        //设置背景容器颜色
        setBackgroundColor(Color.alpha(255));
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        //设置子组件（此处为TextView）的位置和大小
        View textView = getChildAt(0);
        textView.layout(50, 50, textView.getMeasuredWidth() + 50, textView.getMeasuredHeight() + 50);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //先测量所有的子组件的大小
        this.measureChildren(widthMeasureSpec, heightMeasureSpec);
        //测量自身的大小，此处直接写死为500*500
        this.setMeasuredDimension(500, 500);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //为容器画一个红色边框
        RectF rectF = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
        rectF.inset(2, 2);
        Paint pain = new Paint(Paint.ANTI_ALIAS_FLAG);
        pain.setStyle(Paint.Style.STROKE);
        pain.setStrokeWidth(2);
        pain.setColor(Color.BLACK);
        Path path = new Path();
        path.addRoundRect(rectF, 20, 20, Path.Direction.CCW);
        canvas.drawPath(path, pain);
        super.onDraw(canvas);

    }


}
